<template>
	<div class="page">
		<template v-if="isLogin">
			<map id="map" :longitude="lng" :latitude="lat" :markers="markers" scale="6" show-location style="width: 100%; height: 100%;"></map>
		</template>		
	</div>
</template>

<script>
	import {HTTP_REQUEST_URL} from '@/config.js'
	import {jsonToQuery} from '@/common/util.js'
	export default{
		name:'',
		data(){
			return {
				isLogin:false,
				lat:'',
				lng:'',
				type:1,
				page:1,
				params:{ 
				},
				markers:[], 
				isStartTimer:true,
				timer:null,
				isDataOver:false
			}
		},
		async onLoad(params){ 
			this.params=params;
			this.isLogin = await this.$loginState('check');
			if(this.params.type==1){
				if(this.isLogin){ 
					this.getLocal();
					this.getNearData();
				}
			}else if(this.params.type==2){
				if(this.isLogin){ 
					this.getLocal();
					this.getMyData();
				}
			}
		},
		async onShow() {	
			 
			 
		},
		methods:{ 
			
			// 获取当前位置信息
			getLocal: function() {			
				let _this = this;
				uni.getLocation({
					type: 'wgs84',
					geocode: true,
					success: function(res) {
						_this.lat = res.latitude;
						_this.lng = res.longitude;						 
					}
				});
			},
			
			// 获取附近数据列表
			getNearData:async function(lat,lng){
				let res =await this.$api.ssp_api.getNearData({
					page: this.page,
					lat: this.params.lat,
					lng: this.params.lng,
					fk_status: this.params.fk_status,
					is_app:1
				}) 
				if(res.data.ret==0){
					res.data.data.list.forEach(item=>{
						let marker={};
						marker.latitude=parseFloat(item.gcj02_lat);
						marker.longitude=parseFloat(item.gcj02_lng);
						marker.iconPath='../../static/images/mark4.png';
						marker.width='60';
						marker.height= '40';						
						this.markers.push(marker);
					})  
					if (this.markers.length >= res.data.data.count) { 
						clearTimeout(this.timer);
						this.isStartTimer = false;
						this.isDataOver=0;		
					} else { 
						this.page++; 
						if (this.isStartTimer) {
							clearTimeout(this.timer);
							this.timer = setTimeout(() => {
								this.getNearData();
							}, 1000)
						} 						
					}
					 
				} 
			},
			
			// 获取我的数据列表
			getMyData:async function(lat,lng){
				let res =await this.$api.ssp_api.getMyData({
					page: this.page, 
					status: this.params.status,
					is_app:1
				}) 
				if(res.data.ret==0){
					res.data.data.list.forEach(item=>{
						let marker={};
						marker.latitude=parseFloat(item.gcj02_lat);
						marker.longitude=parseFloat(item.gcj02_lng);
						marker.iconPath='../../static/images/mark4.png';
						marker.width='60';
						marker.height= '40';						
						this.markers.push(marker);
					})  
					if (this.markers.length >= res.data.data.count) { 
						clearTimeout(this.timer);
						this.isStartTimer = false;
						this.isDataOver=0;		
					} else { 
						this.page++; 
						if (this.isStartTimer) {
							clearTimeout(this.timer);
							this.timer = setTimeout(() => {
								this.getMyData();
							}, 1000)
						} 						
					}
					 
				} 
			},
		},
		computed: { 
			
		}, 
		components: {
			
		},
	}
</script>

<style lang="less">
	.page{
		width: 100%;
		height: 100%; 
	}
	#map{
		 
		width: 100%;
		height: 100%; 
	}
</style>
